package it.afs.worklog.application.form;

import java.util.Collections;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.util.ListModel;
import org.apache.wicket.validation.validator.RangeValidator;
import it.afs.worklog.Attivita;
import it.afs.worklog.Cliente;
import it.afs.worklog.Commessa;
import it.afs.worklog.Registro;
import it.afs.worklog.application.LoggedPage;
import it.afs.worklog.application.WorkLogSession;

/**
 * @author Fanta
 */
public final class InsertLogPage extends LoggedPage
{
	private Form<Registro> form;
	private ClienteDropDownChoice cliente;
	private CommessaDropDownChoice commessa;
	private AttivitaDropDownChoice attivita;
	
	public InsertLogPage()
	{
		super();
		addComponents();
	}

	@Override
	protected void addComponents()
	{
		super.addComponents();
		
		add(getHomePageLink());
		
		form = new Form<Registro>("form", new CompoundPropertyModel<Registro>(new Registro(WorkLogSession.getCurrentSession().getLoggedUser())))
		{
			private static final long serialVersionUID = -1285523262376028066L;

			protected void onSubmit()
			{
				Registro log = getModelObject();
				int count = getFactory().getDao().getCountOre(log.getData(), log.getUser());
				
				if(count + log.getOre() > 8)
				{
					error(getString("log.insert.dayover"));
					return;
				}
				
				if(!getFactory().getDao().set(log))
				{
					error(getString("log.insert.error"));
					return;
				}
				
				info(getString("log.insert.true"));
				
				log = new Registro(getModelObject().getUser());
				log.setAttivita(getModelObject().getAttivita());
				log.setData(getModelObject().getData());
				setModelObject(log);
				
				modelChanged();
			};
		};
		add(form);
		
		cliente = new ClienteDropDownChoice("cliente", new Model<Cliente>(), new ListModel<Cliente>(getFactory().getDao().getClienti()));
		cliente.add(new AjaxFormComponentUpdatingBehavior("onchange")
		{
			private static final long serialVersionUID = 8781874865778447546L;

			@Override
			protected void onUpdate(AjaxRequestTarget target)
			{
				target.addComponent(commessa);
			}
		});
		cliente.setNullValid(false);
		form.add(cliente);
		
		LoadableDetachableModel<List<Commessa>> commesseModel = new LoadableDetachableModel<List<Commessa>>()
		{
			private static final long serialVersionUID = -2455723151177396489L;

			@Override
			protected List<Commessa> load()
			{
				if(cliente.getModelObject() != null)
				{
					return getFactory().getDao().getCommesse(cliente.getModelObject());
				}
				else
				{
					return Collections.emptyList();
				}
			}
		};
		
		commessa = new CommessaDropDownChoice("commessa", new Model<Commessa>(), commesseModel);
		commessa.add(new AjaxFormComponentUpdatingBehavior("onchange")
		{
			private static final long serialVersionUID = 7877720777984557406L;

			@Override
			protected void onUpdate(AjaxRequestTarget target)
			{
				target.addComponent(attivita);
			}
		});
		commessa.setNullValid(false);
		commessa.setOutputMarkupId(true);
		form.add(commessa);
		
		LoadableDetachableModel<List<Attivita>> attivitaModel = new LoadableDetachableModel<List<Attivita>>()
		{
			private static final long serialVersionUID = 2800196870406324148L;

			@Override
			protected List<Attivita> load()
			{
				if(commessa.getModelObject() != null)
				{
					return getFactory().getDao().getAttivita(commessa.getModelObject());
				}
				else
				{
					return Collections.emptyList();
				}
			}
		};
		
		attivita = new AttivitaDropDownChoice("attivita", attivitaModel);
		attivita.setOutputMarkupId(true);
		attivita.setNullValid(false);
		attivita.setRequired(true);
		form.add(attivita);
		
		form.add(new ShortDateTextField("data").setRequired(true));
		
		form.add(new RequiredTextField<Double>("ore", Double.class)
			.add(new RangeValidator<Double>(0d, 8d)));
		
		form.add(new TextField<Double>("straordinari", Double.class));
		
		form.add(new TextField<Double>("spese", Double.class));
		
		form.add(new FeedbackPanel("feedback"));
	}
}